﻿using CodeMan.DbOther;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace CodeMan.EntityFrameworkCore.Other.DbOther
{
    public class OtherDBRepository : EfCoreRepository<CodeManOtherDbContext, TableEntity>, IOtherDBRepository
    {
        public OtherDBRepository(IDbContextProvider<CodeManOtherDbContext> dbContextProvider)
       : base(dbContextProvider)
        {
        }


        public async Task<List<TableEntity>> GetAllTables(string tkey)
        {
            var dbcontent = await GetDbContextAsync();

            List<TableEntity> result;


            if (tkey.IsNotNullOrWhiteSpace())
            {
                result = await dbcontent.Set<TableEntity>().FromSqlRaw(string.Format(getAllTable, $" where d.name = '{tkey}' ")).ToListAsync();
            }
            else
            {
                result = await dbcontent.Set<TableEntity>().FromSqlRaw(string.Format(getAllTable, "")).ToListAsync();

            }
            return result;
        }

        public async Task<bool> SetConnectionString(string connstr)
        {
            ChangeDBEven.options.ConnectionStrings[ChangeDBEven.ConnectionStringName] = connstr;

            var dbcontent = await GetDbContextAsync();

            await Task.CompletedTask;
            return true;
        }

        string getAllTable = @"SELECT
                                        row_number() over(order by d.name) as Id,
                                        d.name as [Name],
                                        [Namespace]       = case when a.colorder=1 then d.name else '' end,
                                        --[Desc]     = case when a.colorder=1 then isnull(f.value,'') else '' end,
                                        [SortNo]   = a.colorder,
                                        AName     = a.name,
                                        IsIdentity       = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
                                        IsKey       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
                                                         SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
                                        [AType]       = b.name,
                                        [Size]      = a.length,
                                        [Length]       = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
                                        pointSize   = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
                                        [isNull]     = case when a.isnullable=1 then '√'else '' end,
                                        [defaultValue]     = isnull(e.text,''),
                                        ADesc   = isnull(g.[value],'')
                                    FROM 
                                        syscolumns a
                                    left join 
                                        systypes b 
                                    on 
                                        a.xusertype=b.xusertype
                                    inner join 
                                        sysobjects d 
                                    on 
                                        a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
                                    left join 
                                        syscomments e 
                                    on 
                                        a.cdefault=e.id
                                    left join 
                                    sys.extended_properties   g 
                                    on 
                                        a.id=G.major_id and a.colid=g.minor_id  
                                    left join 
  
                                    sys.extended_properties f
                                    on 
                                        d.id=f.major_id and f.minor_id=0
                                    {0}
                                    --where 
                                        --d.name='Tbasyfy1'    --如果只查询指定表,加上此条件
	                                    --a.name LIKE '%name%'
                                    order by 
                                        a.id,a.colorder";
    }
}
